﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Threading;
using LocalSettings;
using QuickTasks.ViewModels.Data;

namespace QuickTasks
{
    public partial class Browsers : PhoneApplicationPage
    {
        
        QuickLinks objQuickLinks = new QuickLinks();
        DispatcherTimer timer = null;

        public Browsers()
        {
            InitializeComponent();
           
            timer = new DispatcherTimer();
            double interval = Convert.ToDouble(IsolatedSettings.getSettingsStringValue("RefreshRate"));
            timer.Interval = TimeSpan.FromMinutes(interval);
            timer.Tick += new EventHandler(timer_Tick);
            if (interval > 0)
                timer.Start();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                ApplicationIdleModeHelper.Current.Locked += new EventHandler(Current_Locked);
                ApplicationIdleModeHelper.Current.UnLocked += new EventHandler(Current_UnLocked);
                string linkId = "";
                if (NavigationContext.QueryString.TryGetValue("linkId", out linkId))
                {
                    IsolatedSettings.saveSettings("linkId", linkId);
                    LoadLinks(objQuickLinks.GetLinks(linkId));
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded event In Browser.xaml page", ex);
            }
        }

        void timer_Tick(object sender, EventArgs e)
        {
            LoadLinks(objQuickLinks.GetLinks(IsolatedSettings.getSettingsStringValue("linkId")));
        }

        void Current_UnLocked(object sender, EventArgs e)
        {

        }

        void Current_Locked(object sender, EventArgs e)
        {

        }

        private void LoadLinks(List<string> links)
        {
            int i = 1;
            foreach (string str in links)
            {
                string url = "http://" + str;
                if (i == 1)
                    webbrowserLink1.Source = new Uri(url, UriKind.RelativeOrAbsolute);
                if (i > 1)
                    LoadBrowsers(url, i);
                i += 1;
                if (i == 7)
                    break;
            }
        }

        private void LoadBrowsers(string url, int i)
        {
            PanoramaItem panoramaItem = new PanoramaItem();
            TextBlock tblk = new TextBlock();
            tblk.Margin = new Thickness(0, 10, 0, 0);
            tblk.FontSize = 20;
            tblk.Text = "URL " + i.ToString();
            panoramaItem.Header = tblk;
            WebBrowser wb = new WebBrowser();
            wb.Height = 680;
            wb.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            wb.Source = new Uri(url, UriKind.RelativeOrAbsolute);
            panoramaItem.Content = wb;
            panoramaBrowser.Items.Add(panoramaItem);
        }
    }
}